<?php
########################################################
# DMS Donations Management System v1.0                 #
# created by:  Isaac Sabas                             #
#			   Nikko Reyes							   #
#			   Miko Tiamwatt						   #
#                                                      #
# De La Salle Philippines                              #
# All Rights Reserved DLSP Copyright 2009              #
########################################################

define('ACCESS', true);	

session_start();	
	
# set error checking levels #	
	# enable these lines when site is to be deployed #	
	ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'On');
    //ini_set('log_errors', 'On');
    //ini_set('error_log', '/usr/local/apache/logs/error_log');
	
	#############################
	# include some needed files #
	#############################
		$absRoot = '../../';
		include($absRoot . 'lib/XAJAX/xajax_core/xajax.inc.php');
		include($absRoot . 'src/ClientHandle.php');

	$id = $_GET["client_id"];

	$client = new ClientHandle();
	
$AddressesArray=$client->getAddresses($id);

	
# instantiate predifined classes #											
	$xajax = new xajax();

	$xajax->registerFunction("editClientCompanyAddress");
	
		function editClientCompanyAddress($addressid, $street, $municipality, $city, $province, $country, $zipcode, $id) {
		$statusresponse = new xajaxResponse();
	
		# check if the fields are empty
			if (empty($street) || empty($municipality) || empty($city) || empty($province) || empty($country) || empty($zipcode)) {
				$statusresponse->assign('Status','style.color','red');
				$statusresponse->assign( 'Status', 'innerHTML', 'Please complete all fields.<br /> Not updating..');
			}
			else {	
			
			$client = new ClientHandle();

			if($addressid==-1)
			{
			$client = new ClientHandle();
			
			$Address['address_street']=$street;
			$Address['address_city']=$city;
			$Address['address_municipality']=$municipality;
			$Address['address_province']=$province;
			$Address['address_country']=$country;
			$Address['address_zipcode']=$zipcode;
			$Address['address_type']="Work";
			
			$result=$client->addNewAddress($Address,$id);	
			
			if ($id != '') {
							
						
						if($result == true)
						{
							$statusresponse->call("parent.window.frames['view_client_profile_".$id."'].location.reload()"); 
							$statusresponse->assign('Status','style.color','green');
							$statusresponse->assign('Status', 'innerHTML', 'Client Company Address changed.<br /> Database updated.');
						}
						else
						{
							$statusresponse->assign('Status','style.color','red');
							$statusresponse->assign('Status', 'innerHTML', 'Database error.<br />Not updating..');
						}
				
			}
			}
			else
			{
			$AddressesArray=$client->getAddresses($id);
			
			$Address=$AddressesArray[$addressid];
							
			$AddId=$Address['address_id'];
		
			$Street=$Address['address_street'];
			
			$Municipality=$Address['address_municipality'];
			
			$City=$Address['address_city'];
			
			$Province=$Address['address_province'];
			
			$Country=$Address['address_country'];
			
			$ZipCode=$Address['address_zipcode'];
			
					if ($Street == $street && $Municipality == $municipality && $City == $city && $Province == $province && $Country == $country && $ZipCode == $zipcode) {
						$statusresponse->assign('Status','style.color','orange');
						$statusresponse->assign('Status', 'innerHTML', 'No changes are made.<br /> Not updating..');
					}
					else if ($id != '') {
							
						$result = $client->setAddressValue($AddId, 'address_street', $street, $id);
						$result = $client->setAddressValue($AddId, 'address_municipality', $municipality, $id);
						$result = $client->setAddressValue($AddId, 'address_city', $city, $id);
						$result = $client->setAddressValue($AddId, 'address_province', $province, $id);
						$result = $client->setAddressValue($AddId, 'address_country', $country, $id);
						$result = $client->setAddressValue($AddId, 'address_zipcode', $zipcode, $id);
						$result = $client->setAddressValue($AddId, 'address_type', 'Work', $id);						
						
						if($result == true)
						{
							$statusresponse->call("parent.window.frames['view_client_profile_".$id."'].location.reload()"); 
							$statusresponse->assign('Status','style.color','green');
							$statusresponse->assign('Status', 'innerHTML', 'Client Company Address changed.<br /> Database updated.');
						}
						else
						{
							$statusresponse->assign('Status','style.color','red');
							$statusresponse->assign('Status', 'innerHTML', 'Database error.<br />Not updating..');
						}
					}
			
			}
			
			}
								
		return$statusresponse;
	}
	
	$xajax->processRequest();	


	//print_r($result);
?>

<html>
<head>
	<script type="text/javascript" src="../../lib/XAJAX/xajax_js/xajax_core.js" />
	<link rel="stylesheet" type="text/css" href="../../lib/YUI/yui/build/fonts/fonts-min.css" />
	<?php $xajax->printJavaScript(); ?>	
</head>
<body>											

<form id="EditClientCompanyAddresses" action="#" method='post'>
<table>
<?php
$adrsctr=0;
$initial=-1;	
while($adrsctr < sizeof($AddressesArray))
{
$Temp=$AddressesArray[$adrsctr];
if ($Temp['address_type']=="Work")
{
if($initial==-1)
{
$initial=$adrsctr;
$ToEdit=$Temp;
}
}
$adrsctr++;
}
if($initial==-1)
{
$ToEdit['address_street']="";
$ToEdit['address_municipality']="";
$ToEdit['address_city']="";
$ToEdit['address_province']="";
$ToEdit['address_country']="";
$ToEdit['address_zipcode']="";	
}
?>
	<tr>
	<td>
	Street:</td>
	<td>
		<input type=text id="Street" name="Street" value="<?php echo $ToEdit['address_street'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Municipality:</td>
	<td>
		<input type=text id="Municipality" value="<?php echo $ToEdit['address_municipality'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	City:</td>
	<td>
	<input type=text id="City" value="<?php echo $ToEdit['address_city'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Province:</td>
	<td>
	<input type=text id="Province" value="<?php echo $ToEdit['address_province'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Country:</td>
	<td>
	<input type=text id="Country" value="<?php echo $ToEdit['address_country'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	Zip Code:</td>
	<td>
	<input type=text id="ZipCode" value="<?php echo $ToEdit['address_zipcode'];?>"></input>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	</tr>
	<tr>
	<td>
	</td>
	<td>
	<input type=button id="Update" value="Update" onclick="xajax_editClientCompanyAddress(<?php echo $initial;?>, document.getElementById('Street').value, document.getElementById('Municipality').value,document.getElementById('City').value,document.getElementById('Province').value, document.getElementById('Country').value,document.getElementById('ZipCode').value, <?php echo $id;?>);"></input>
	<input type=reset id="Reset" value="Reset"></input>
	</td>
	</tr>
	
</table>
</form>

<div id='Status'></div>

</body>

</html> 